package com.woman.beautylive.presentation.ui.main.find.adapter;

import android.app.Activity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.woman.beautylive.R;
import com.woman.beautylive.presentation.ui.main.find.String2Spanned;
import com.woman.beautylive.presentation.ui.main.find.paihangbangbean.DongTaiListBean;

import java.util.List;

/**
 * Created by admin on 2016/9/25.
 */
public class ReplyAdapter extends BaseAdapter {

    List<DongTaiListBean.DongTaiItem.ReplyList.ReplyItem> list;
    Activity activity;
    int parentPosition;
    public ReplyAdapter(int parentPosition, List<DongTaiListBean.DongTaiItem.ReplyList.ReplyItem> list, Activity activity) {
        this.list = list;
        this.activity = activity;
        this.parentPosition = parentPosition;
    }

    @Override
    public int getCount() {
        if(list!=null){
            if(list.size()<=4) {
                return list.size();
            }
            else{
                return 5;
            }
        }
        return list == null ? 0 : 5;
    }

    @Override
    public Object getItem(int position) {
        return list == null ? 0 : list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        Holder holder = null;
        if (convertView == null) {
            holder = new Holder();
            convertView = LayoutInflater.from(activity).inflate(R.layout.item_pinglun, null);
            holder.tv_author_1 = (TextView) convertView.findViewById(R.id.tv_author_1);
            holder.tv_author_2 = (TextView) convertView.findViewById(R.id.tv_author_2);
            holder.tv_huifu = (TextView) convertView.findViewById(R.id.tv_huifu);
            holder.tv_content = (TextView) convertView.findViewById(R.id.tv_content);
            convertView.setTag(holder);
        } else {
            holder = (Holder) convertView.getTag();
        }

        holder.tv_author_1.setVisibility(View.GONE);
        holder.tv_author_2.setVisibility(View.GONE);
        holder.tv_huifu.setVisibility(View.GONE);

        if (list.get(position).getTouid() != null) {
            if(position<=3) {
                if (list.get(position).getTorid().equals("0")) {
                    holder.tv_content.setText(String2Spanned.parsed("<font color=#FF6EA5>" + list.get(position).getNickname() + ":  </font>" + list.get(position).getContent()));
                } else {
//                holder.tv_author_1.setText(list.get(position).getNickname());
//
//                holder.tv_author_2.setVisibility(View.VISIBLE);
//                holder.tv_author_2.setText(list.get(position).getTonickname() + ":");
                    holder.tv_content.setText(String2Spanned.parsed("<font color=#FF6EA5>" + list.get(position).getNickname() + "</font>回复 <font color=#FF6EA5>" + list.get(position).getTonickname() + ":</font>" + list.get(position).getContent()));

//                holder.tv_huifu.setVisibility(View.VISIBLE);
                }
            }else{
                holder.tv_content.setText("查看更多>>");
//                holder.tv_content.setTag(list.get(position).getPid());
                holder.tv_content.setGravity(Gravity.CENTER);
                holder.tv_content.setTag("more");
//                holder.tv_content.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        Intent intent=new Intent(activity, DongTaiDetailActivity.class);
//                        intent.putExtra("pid",v.getTag()+"");
//                        activity.startActivity(intent);
//
//                    }
//                });
            }
        }
        convertView.setTag(R.id.tag_3, parentPosition);//记录父类的position
        convertView.setTag(R.id.tag_4,position);//记录自己的position
        convertView.setTag(R.id.tag_1, list.get(position).getUid());
        convertView.setTag(R.id.tag_2, list.get(position).getRid());
        return convertView;
    }


    class Holder {
        TextView tv_author_1, tv_author_2, tv_huifu, tv_content;
    }
}
